// pages/ddjs/ddjs.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[],
    //总价个
    all_price:"",
    //总数量
    all_num:"",
    goods_id:'',
    shop_id:'',
    num:'',
    spec_id:'',
    order_start:"1",//立即购买状态1 购物车状态2
    shopping_id:'',//商品购物车id
    user:'',//用户信息
    address:[],//用户地址信息
    userinfo: {} //提交的数据

   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;

    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo){
      console.log(userInfo);
      this.setData({
        user: userInfo
      })
    }
    //购物车结算
    if(options.shopping_id != undefined){
      app.wxRequest('POST','/Home/Goods/order',{'shopping_id':options.shopping_id,'status':'1'},function(res){
        if(res.data.code == 1){
          that.setData({
            data:res.data.data,
            all_price:res.data.all_price,
            all_num:res.data.all_num,
            order_start:'2',
            shopping_id:options.shopping_id
          })

        }
      })
    }else{
      //商品直接结算
        that.setData({
          'goods_id':options.goods_id,
          'shop_id':options.shop_id,
          'num':options.num,
          'spec_id':options.spec_id
        })
        app.wxRequest('POST','/Home/Goods/order',{'goods_id':options.goods_id,'shop_id':options.shop_id,'num':options.num,'spec_id':options.spec_id,'status':'0'},function(res){
          if(res.data.code == 1){
            that.setData({
              data:res.data.data,
              all_price:res.data.all_price,
              all_num:res.data.all_num
            })

          }
        },function(err){

        })
    }

    //获取用户地址信息
    var userId = that.data.user.id;
    app.wxRequest('POST','/Home/User/choole_address',{
      'user_id':userId
    },function(res){
      if(res.data.code == 1){
        that.setData({
          address:res.data.data
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取用户备注信息
  inputWatch:function (e) {
    console.log(e.currentTarget);
    let userinfo = this.data.userinfo;
    let item = e.currentTarget.dataset.name;
    userinfo[item] = e.detail.value;
    this.setData({
      userinfo
    });
    console.log('userinfo', userinfo)
  },
  //实时获取数据

  sub(param) {
    var that = this;
    var order_com = JSON.stringify(that.data.userinfo);

    var goods_id = that.data.goods_id;
    var shop_id= that.data.shop_id;
    var num = that.data.num;
    var spec_id = that.data.spec_id;
    var order_start = that.data.order_start
    var shopping_id = that.data.shopping_id;
    var address = that.data.address;
    var user = that.data.user;
    console.log(address);
    
    if(address == ''){
        //如果收货地址为空则让区填写收货地址
        wx.showModal({
          title: '提示',
          content: '请先填写收货地址',
          success (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/shdigl/shdigl'
              });
            } 
          }
        })
        return false;
    }
    if(order_start == 1 ){
      //单商品购买
      app.wxRequest('POST','/Home/Goods/sub_order',{
        'goods_id':goods_id,
        'shop_id':shop_id,
        'num':num,
        'spec_id':spec_id,
        "order_start":order_start,
        "addressId":address.id,
        "userId":user.id,
        "order_com":order_com
        //"openId":user.openid
      },function(res){     
        console.log(res);
        if(res.data.code == 1){
            wx.requestPayment(
            {
              'timeStamp': res.data.data.parameters.timeStamp,
              'nonceStr': res.data.data.parameters.nonceStr,
              'package':  res.data.data.parameters.package,
              'signType': 'MD5',
              'paySign':  res.data.data.parameters.paySign,
              'success':function(res){
                console.log(res);
              },
              'fail':function(res){
                console.log(res)
              },
              'complete':function(res){}
            })

            /*wx.navigateTo({
              //url: '/pages/sp_pay/sp_pay?order_id='+res.data.id,
            })*/
        }
      })
    }else{
      app.wxRequest('POST','/Home/Goods/sub_order',{
        'shopping_id':shopping_id,
        "order_start":order_start,
        "addressId":address.id,
        "userId":user.id,
        "order_com":order_com
        //"openId":user.openid
      },function(res){     
        console.log(res);
        if(res.data.code == 1){
          wx.requestPayment(
            {
              'timeStamp': res.data.data.parameters.timeStamp,
              'nonceStr': res.data.data.parameters.nonceStr,
              'package':  res.data.data.parameters.package,
              'signType': 'MD5',
              'paySign':  res.data.data.parameters.paySign,
              'success':function(res){
                console.log(res);
              },
              'fail':function(res){
                console.log(res)
              },
              'complete':function(res){}
            })
        }
      })
    }
  }
})